package com.intramirror.product.controller;

import com.intramirror.core.common.exception.ValidateException;
import com.intramirror.core.common.response.ErrorResponse;
import com.intramirror.core.common.response.Response;
import com.intramirror.core.common.response.StatusCode;
import com.intramirror.product.entity.FeaturedBrand;
import com.intramirror.product.service.IFeaturedBrandService;
import com.intramirror.product.utils.ConvertUtil;
import java.util.List;
import java.util.Map;
import javax.xml.bind.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

/**
 * Created on 2018/7/3.
 * @author 123
 */
@RestController
public class FeaturedBrandController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FeaturedBrandController.class);

    @Autowired
    IFeaturedBrandService featuredBrandService;

    @GetMapping(value = "/featured/brand")
    public Response getFeaturedBrandList() {
        LOGGER.debug("Start to get featured brand list.");
        return Response.status(StatusCode.SUCCESS).data(featuredBrandService.getFeaturedBrandList());
    }

    @PutMapping(value = "/featured/brand")
    public Response updateFeaturedBrand(@SessionAttribute(value = "user") Map<String, Object> user, @RequestBody FeaturedBrand featuredBrand)
            throws ValidationException {
        Long userId = ConvertUtil.toLong(user.get("userId"));
        LOGGER.debug("Start to update featured brand, user id {}.", userId);
        if (featuredBrand == null || featuredBrand.getFeaturedBrandId() == null) {
            throw new ValidateException(new ErrorResponse("Featured brand id is required."));
        }

        featuredBrandService.updateFeaturedBrand(featuredBrand);
        return Response.status(StatusCode.SUCCESS).build();
    }

    @PostMapping(value = "/featured/brand")
    public Response addFeatureBrandList(@SessionAttribute(value = "user") Map<String, Object> user, @RequestBody List<FeaturedBrand> featuredBrandList) {
        Long userId = ConvertUtil.toLong(user.get("userId"));
        LOGGER.debug("Start to refresh all feature brand list, user id is {}.", userId);
        Long t1 = System.currentTimeMillis();
        for (FeaturedBrand featuredBrand : featuredBrandList) {
            if (featuredBrand.getBrandId() == null) {
                throw new ValidateException(new ErrorResponse("Featured brand id is required."));
            }

            if (featuredBrand.getShow() == null) {
                throw new ValidateException(new ErrorResponse("Featured brand id is required."));
            }

            if (featuredBrand.getBrandName() == null || "".equals(featuredBrand.getBrandName())) {
                throw new ValidateException(new ErrorResponse("Featured brand name is required."));
            }

            if (featuredBrand.getSortNum() == null) {
                throw new ValidateException(new ErrorResponse("Featured sort num is required."));
            }
        }
        featuredBrandService.addFeatureBrandList(featuredBrandList);
        List<FeaturedBrand> result = featuredBrandService.getFeaturedBrandList();

        LOGGER.info("|==============>total takes {}ms.", System.currentTimeMillis() - t1);
        return Response.status(StatusCode.SUCCESS).data(result);
    }
}
